﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using LPV_POS.Module;
using LPV_POS.In;

namespace LPV_POS.mh_pos.mh_danhmuc
{
    public partial class mh_dmlistneed : DevExpress.XtraEditors.XtraForm
    {
        thuvien tv;
        public string str_tenhanghoa;
        public string str_mahanghoa;
        public string str_dvt;
        cpublic ocpublic;
        int i1, i2, i3;
         xulyngaythangnam ngaythang;
        string Print_Align_Left = ((char)(27)).ToString() + ((char)(97)).ToString() + '\0';
        

        private DataTable f_select_listneed(string mahh, float soluong)
        {
           DataTable tb;
            tv = new thuvien();
            tb = tv.SP_SelectDataTable("sp_dinhluong", 
                                        new string[] { "@flag", "@choose", "@mahh", "@soluong" }, 
                                        new object[] { 1, 3, mahh, soluong });
            return tb;
        }

        string f_splitString4Column(string a, string b, string c, string d, int i1, int i2, int i3)
        {//i1: 32; i2:7; i3: 17
            string str = "";
            str = a;
            str += new cpublic().Spacestring(i1 - a.Length - b.Length);
            str += b;
            str += new cpublic().Spacestring(i2 - c.Length);
            str += c;
            str += new cpublic().Spacestring(i3 - d.Length);
            str += d;
            return str;
        }
        // ngay thang hien hanh
        private string f_chuoingayhienhanh()
        {
            string ngaythangnam = "";
            ngaythang = new xulyngaythangnam();
            tv = new thuvien();
            
            DataTable tb = tv.SP_SelectDataTable("sp_mhcompany", new string[] { "@flag", "@CHOOSE" }, new object[] { 1, 1 });
            if (tb.Rows.Count > 0)
                ngaythangnam = ngaythang.chuoingay(tb.Rows[0]["NGAYHIENHANH"] + "");
            return ngaythangnam;
        }

        #region "In listneed"
        
        // in toan bo phieu thu 
        private int f_print_listneed(string mathang, DataTable tb_nguyenlieu)
        {
            ocpublic = new cpublic();
            i1 = 28;
            i2 = 14;
            i3 = 14;
            string tencongty = "";
            string dienthoai = "";
            string diachi = "";

            Mod_header_report header_report = new Mod_header_report();
            DataTable tb;

            tb = header_report.f_selectheaderreport();
            if (tb.Rows.Count > 0)
            {
                tencongty = tb.Rows[0]["HEADER"] + "";
                dienthoai = tb.Rows[0]["PHONE"] + "";
                diachi = tb.Rows[0]["ADDRESS"] + "";
            }

            //Boolean ketqua;
            //.	Khởi tạo Class: 
            PrintCOMs oPrintCOM = new PrintCOMs(PrintCOMs.Print_Type.Print_BILL);
            //	Start máy in: 
            SerialPort_LPV[] oSerialPort_LPVs = oPrintCOM.Print_Start();

            foreach (SerialPort_LPV oSerialPort_LPV in oSerialPort_LPVs)
            {

                if (oSerialPort_LPV != null)
                {
                    oSerialPort_LPV.Write(Print_Align_Left);
                   
                    oPrintCOM.Print_Line(oSerialPort_LPV, PrintCOMs.aligns.Align_Left, PrintCOMs.Fonts.Font_Normal, ' ', PrintCOMs.Fills.Fills_Full);

                    if (tencongty != "")
                        oPrintCOM.Print_Content(oSerialPort_LPV, PrintCOMs.aligns.Align_Left, PrintCOMs.Fonts.Font_Normal, tencongty, "", "", "");

                    if (dienthoai != "")
                        oPrintCOM.Print_Content(oSerialPort_LPV, PrintCOMs.aligns.Align_Left, PrintCOMs.Fonts.Font_Normal, dienthoai, "", "", "");

                    if (diachi != "")
                        oPrintCOM.Print_Content(oSerialPort_LPV, PrintCOMs.aligns.Align_Left, PrintCOMs.Fonts.Font_Normal, diachi, "", "", "");

                    oPrintCOM.Print_Content(oSerialPort_LPV, PrintCOMs.aligns.Align_Center, PrintCOMs.Fonts.Font_Normal, "Danh sach nguyen lieu can lam " + float.Parse( txt_soluong.Text).ToString("N0") + ' ' + str_dvt, "", "", "");
                    oPrintCOM.Print_Content(oSerialPort_LPV, PrintCOMs.aligns.Align_Center, PrintCOMs.Fonts.Font_Double_Height, mathang.ToUpper(), "", "", "");
                    oPrintCOM.Print_Line(oSerialPort_LPV, PrintCOMs.aligns.Align_Center, PrintCOMs.Fonts.Font_Normal, '=', PrintCOMs.Fills.Fills_Full);
                    oSerialPort_LPV.WriteLine(this.f_splitString4Column("Nguyen lieu", "", "So luong", "DVT", i1, i2, i3));
               
                    string tennguyenlieu = "";
                    string soluong = "";
                    string dvt = "";

                    for (int i = 0; i < tb_nguyenlieu.Rows.Count; i++)
                    {
                        tennguyenlieu = ocpublic.RemoveSign4VietnameseString(tb_nguyenlieu.Rows[i]["TENNGUYENLIEU"] +"");

                        string soluong_temp = tb_nguyenlieu.Rows[i]["SOLUONGCAN"] + "";
                        if(soluong_temp.Contains("."))
                            soluong = float.Parse(tb_nguyenlieu.Rows[i]["SOLUONGCAN"] + "").ToString("N4");
                        else
                            soluong = float.Parse(tb_nguyenlieu.Rows[i]["SOLUONGCAN"] + "").ToString("N0");

                        //soluong = float.Parse(tb_nguyenlieu.Rows[i]["SOLUONGCAN"] + "").ToString("N4");
                        
                        dvt = ocpublic.RemoveSign4VietnameseString(tb_nguyenlieu.Rows[i]["TENDVT"] + "");

                        oSerialPort_LPV.WriteLine(this.f_splitString4Column(tennguyenlieu, "", soluong, dvt, i1, i2, i3));

                        //   oPrintCOM.Print_Content(PrintCOM.aligns.Align_Left, PrintCOM.Fonts.Font_Normal, lydothu,  ngay, sotien,"");
                    }

                    oPrintCOM.Print_Line(oSerialPort_LPV, PrintCOMs.aligns.Align_Center, PrintCOMs.Fonts.Font_Normal, '=', PrintCOMs.Fills.Fills_Full);
                    oSerialPort_LPV.WriteLine(this.f_splitString4Column("In bao cao " + ocpublic.RemoveSign4VietnameseString(f_chuoingayhienhanh().ToLower()), "", "", "", i1, i2, i3));
                    oPrintCOM.Print_Line(oSerialPort_LPV, PrintCOMs.aligns.Align_Center, PrintCOMs.Fonts.Font_Normal, ' ', PrintCOMs.Fills.Fills_Full);
                    oPrintCOM.Print_Bottom(oSerialPort_LPV);//
                    oPrintCOM.Print_Close();
                }

            }
            //	Start máy in: 
            return 1;
        }

        #endregion

        public mh_dmlistneed()
        {
            InitializeComponent();
        }

        private void mh_dmlistneed_Load(object sender, EventArgs e)
        {
            txt_tenhanghoa.Text = str_tenhanghoa;
            lbel_dvt.Text = str_dvt.Trim();
            bton_in.Enabled = false;
        }

        private void bton_soluong_Click(object sender, EventArgs e)
        {
            string hanghoa_soluong = new cpublic().loadformso_point(550, 270);
            if (hanghoa_soluong != "")
                txt_soluong.Text = hanghoa_soluong;
        }

        private void bton_xem_Click(object sender, EventArgs e)
        {
            if (txt_soluong.Text != "")
            {
                float soluong = 1;
                float.TryParse(txt_soluong.Text, out soluong);
                DataTable tb = f_select_listneed(str_mahanghoa, soluong);
                luoi_nguyenlieucan.DataSource = tb;
            }
        }

        private void bton_thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bton_in_Click(object sender, EventArgs e)
        {
            if (grid_nguyenlieucan.RowCount < 0)
            {
                Mod_messagebox mess = new Mod_messagebox();
                mess.messagebox_pos("Thông báo", "Chưa có dữ liệu", loaithongbao.thongtin.GetHashCode());
                return;
            }
            DataTable tb = (DataTable)luoi_nguyenlieucan.DataSource;
            ocpublic = new cpublic();
            f_print_listneed(ocpublic.RemoveSign4VietnameseString(str_tenhanghoa),tb);
        }

        private void grid_nguyenlieucan_DataSourceChanged(object sender, EventArgs e)
        {
            if (grid_nguyenlieucan.RowCount > 0)
                bton_in.Enabled = true;
            else
                bton_in.Enabled = false;
        }
    }
}